<template>
  <view class="admin-dashboard">
    <!-- 登录表单 - 未登录时显示 -->
    <view class="login-container" v-if="!isLoggedIn">
      <view class="login-card">
        <view class="login-header" :style="loginHeaderStyle"></view>

        <view class="login-title">管理员登录</view>

        <view class="form-group">
          <text class="form-label">账号</text>
          <input
            type="text"
            v-model="username"
            class="form-input"
            placeholder="请输入账号" />
        </view>

        <view class="form-group">
          <text class="form-label">密码</text>
          <input
            type="password"
            v-model="password"
            class="form-input"
            placeholder="请输入密码" />
        </view>

        <button class="login-btn" @click="handleLogin">登录</button>
      </view>
    </view>

    <!-- 管理内容区域 - 登录成功后显示 -->
    <view v-else>
      <!-- 欢迎语区域 -->
      <view class="welcome-section">
        <text class="welcome-title">欢迎回来，管理员</text>
        <text class="date-tip">
          今天是 {{ currentDate }}，请选择您要管理的内容
        </text>
        <button class="logout-btn" @click="handleLogout">退出登录</button>
      </view>

      <!-- 管理卡片 -->
      <view
        class="management-card"
        v-for="(item, index) in managelist"
        :key="index"
        @click="handleCardClick(item.type)">
        <view class="card-header" :style="item.headerStyle"></view>
        <view class="card-title">{{ item.title }}</view>
        <view class="card-desc">{{ item.desc }}</view>
        <view class="enter-btn">进入管理</view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, getCurrentInstance } from "vue";

const { proxy } = getCurrentInstance();

// 登录相关状态
const isLoggedIn = ref(false);
const username = ref("");
const password = ref("");

const currentDate = ref("");
const currentUser = ref({
  username: "",
  id: "",
  // 可以添加其他用户信息字段
});

// 登录头部样式，与现有卡片风格统一
const loginHeaderStyle = {
  background: "linear-gradient(to right, #ff9f4a, #ff6b6b)",
};

// 管理列表数据
const managelist = ref([
  {
    title: "分类管理",
    desc: "创建、编辑分类，支持拖拽排序",
    type: "category",
    headerStyle: {
      background: "linear-gradient(to right, #ffcaa7e6, #ef476fe6 100%)",
    },
  },
  {
    title: "商品管理",
    desc: "添加、编辑商品信息",
    type: "product",
    headerStyle: {
      background: "linear-gradient(to right, #ffd166e6, #ef476fe6 100%)",
    },
  },
]);

// 格式化日期函数
const formatDate = (date: Date): string => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const week = ["日", "一", "二", "三", "四", "五", "六"][date.getDay()];
  return `${year}年${month}月${day}日 星期${week}`;
};

// 初始化 - 检查是否已登录
onMounted(() => {
  currentDate.value = formatDate(new Date());
  // 检查本地存储中是否有登录信息
  const storedUser = uni.getStorageSync("adminUser");

  if (storedUser) {
    isLoggedIn.value = true;
  }
});

// 处理登录
const handleLogin = async () => {
  // 简单验证
  if (!username.value || !password.value) {
    uni.showToast({
      title: "请输入",
      icon: "error",
      duration: 200,
    });
    return;
  }

  if (username.value == "gaoruiqing" && password.value == "123456") {
    uni.setStorageSync("adminUser", "gaoruiqing");
    // 更新状态
    isLoggedIn.value = true;
    password.value = "";
    username.value = "";
    uni.showToast({
      title: "登录成功",
      icon: "success",
      duration: 200,
    });
  } else {
    uni.showToast({
      title: "登录错误",
      icon: "error",
      duration: 200,
    });
  }
};

// 处理退出登录
const handleLogout = () => {
  uni.showModal({
    title: "确认退出",
    content: "确定要退出登录吗？",
    success: (res) => {
      if (res.confirm) {
        // 清除所有登录相关信息
        uni.removeStorageSync("adminUser");

        // 更新状态
        isLoggedIn.value = false;

        uni.showToast({
          title: "已退出登录",
          icon: "none",
          duration: 200,
        });
      }
    },
  });
};

// 处理卡片点击
const handleCardClick = (type: string) => {
  proxy.$tab.navigateTo(`/pages/manage/${type}`);
};
</script>

<style lang="scss" scoped>
.admin-dashboard {
  padding: 20px;
  background-color: #fff8f0;
  min-height: 100vh;
  overflow: hidden;

  // 登录容器样式
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: calc(100vh - 40px); // 减去padding
  }

  // 登录卡片样式，与管理卡片风格统一
  .login-card {
    width: 100%;
    max-width: 400px;
    border: 0.5px solid #ff8a3c4a;
    border-radius: 12px;
    box-shadow: 0 10px 25px -5px rgba(255, 138, 60, 0.1),
      0 10px 10px -5px rgba(255, 138, 60, 0.04);
    overflow: hidden;

    .login-header {
      height: 40px;
      width: 100%;
    }

    .login-title {
      font-size: 22px;
      font-weight: 700;
      color: #333;
      padding: 20px 16px 10px;
      display: block;
      text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    .form-group {
      padding: 0 16px;
      margin-bottom: 16px;

      .form-label {
        display: block;
        margin-bottom: 8px;
        font-size: 14px;
        color: #666;
        font-weight: 500;
      }

      .form-input {
        width: 100%;
        height: 40px;
        padding: 0px 5px;
        border: 1px solid #ddd;
        border-radius: 6px;
        font-size: 16px;
        line-height: 16px;
        box-sizing: border-box;

        &:focus {
          outline: none;
          border-color: #ff8a3c;
        }
      }
    }

    .login-btn {
      width: calc(100% - 32px);
      margin: 10px 16px 16px;
      background-color: #ff8a3c;
      color: #fff;
      border: none;
      border-radius: 6px;
      font-size: 16px;
      font-weight: 600;
      cursor: pointer;
      transition: background-color 0.3s;

      &:hover {
        background-color: #ff701c;
      }

      &:disabled {
        background-color: #ffb88c;
        cursor: not-allowed;
      }
    }

    .error-message {
      color: #ff4d4f;
      font-size: 14px;
      padding: 0 16px 16px;
      display: block;
    }
  }

  .welcome-section {
    margin-bottom: 24px;
    padding: 16px;
    background-color: #fff;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    position: relative;

    .welcome-title {
      font-size: 24px;
      font-weight: 700;
      color: #333;
      margin-bottom: 8px;
      display: block;
    }

    .date-tip {
      font-size: 16px;
      font-weight: 500;
      color: #666;
    }

    .logout-btn {
      position: absolute;
      right: 16px;
      top: 16px;
      padding: 0px 5px;
      background-color: #fff8f0;
      color: #666;
      border: none;
      border-radius: 4px;
      font-size: 14px;
      cursor: pointer;

      &:hover {
        background-color: #e5e5e5;
      }
    }
  }

  .management-card {
    margin-bottom: 20px;
    border: 0.5px solid #ff8a3c4a;
    border-radius: 12px;
    cursor: pointer;
    box-shadow: 0 10px 25px -5px rgba(255, 138, 60, 0.1),
      0 10px 10px -5px rgba(255, 138, 60, 0.04);
    transition: transform 0.3s, box-shadow 0.3s;

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 15px 30px -5px rgba(255, 138, 60, 0.15),
        0 10px 10px -5px rgba(255, 138, 60, 0.08);
    }

    .card-header {
      height: 120px;
      margin-bottom: 16px;
      border-radius: 12px 12px 0px 0px;
    }
    .card-title {
      font-size: 18px;
      font-weight: bold;
      padding: 0 16px 10px;
      text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    .card-desc {
      font-size: 14px;
      color: #666;
      margin-bottom: 10px;
      line-height: 1.6;
      padding: 0 16px;
    }

    .enter-btn {
      color: #ff8a3c;
      font-size: 14px;
      cursor: pointer;
      display: inline-block;
      padding: 0 16px 16px;
      position: relative;

      &:after {
        content: "→";
        margin-left: 5px;
        transition: margin-left 0.3s;
      }

      &:hover:after {
        margin-left: 8px;
      }
    }
  }
}
</style>
